package com.cndatacom.teleweb.dao.impl;

import java.util.List;

import com.cndatacom.teleweb.basedaosupport.PaginationDaoSupport;
import com.cndatacom.teleweb.common.PaginationModel;
import com.cndatacom.teleweb.dao.IUserDao;
import com.cndatacom.teleweb.model.User;

@SuppressWarnings("unchecked")
public class UserDao extends PaginationDaoSupport implements IUserDao {

	public User getUserById(int userId) {
		return (User)get(userId);
	}

	public User getUserByAccount(String account) {
		List userList = find("from User as user where user.account=?", account);
		if(userList!=null && userList.size()>=1){
			return (User)userList.get(0);
		}
		return null;
	}

	public User getUserByAccountAndPassword(String account, String password) {
		Object[] params = new Object[]{account,password};
		List userList = find("from User as user where user.account=? and user.password=?", params);
		if(userList!=null && userList.size()>=1){
			return (User)userList.get(0);
		}
		return null;
	}

	public boolean delUser(User user) {
		delete(user);
		return true;
	}

	public PaginationModel getAllUser(int offset, int pageSize) {
		return searchPaginated("select user from com.cndatacom.teleweb.model.User user order by user.id desc", null, offset, pageSize);
	}

	public User saveOrUpateUser(User user) {
		saveOrUpdate(user);
		return user;
	}

}
